package com.colin.aiyi.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.colin.aiyi.R;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.ui.login.ImpPLogin;
import com.colin.aiyi.ui.login.LoginAc;
import com.colin.aiyi.view.ShowMsgUtil;

import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by hl on 2017/5/31.
 */

public class EditPasswdAc extends BaseActivity {
    @BindView(R.id.edit_passwd_old)
    EditText editPasswdOld;
    @BindView(R.id.edit_passwd_new1)
    EditText editPasswdNew1;
    @BindView(R.id.edit_passwd_new2)
    EditText editPasswdNew2;
    @BindView(R.id.button_send)
    Button buttonSend;
    @BindView(R.id.text_banner_left)
    TextView textBannerLeft;
    @BindView(R.id.text_banner_title)
    TextView textBannerTitle;
    @BindView(R.id.text_banner_rigth)
    TextView textBannerRigth;
    @BindView(R.id.text_msg)
    TextView textMsg;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_edit_passwd);
        ButterKnife.bind(this);
        textBannerTitle.setText("修改密码");
        textBannerLeft.setOnClickListener(this);
        user = getUser();
        iPresenter = new ImpPLogin();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.button_send:
                String oldPW = editPasswdOld.getText().toString().replace(" ", "");
                String newPW = editPasswdNew1.getText().toString().replace(" ", "");
                String newPW2 = editPasswdNew2.getText().toString().replace(" ", "");
                if (oldPW == null || oldPW.length() < 1) {
                    textMsg.setText("请输入原密码");
                    //showMsg("请输入原密码");
                    return;
                }
                if (newPW == null || newPW.length() < 1) {
                    textMsg.setText("请输入新密码");
                   // showMsg("请输入新密码");
                    return;
                }
                if (newPW2 == null || newPW2.length() < 1) {
                    textMsg.setText("请重复输入新密码");
                   // showMsg("请重复输入新密码");
                    return;
                }
                if (!newPW.equals(newPW2)) {
                    textMsg.setText("两次输入密码不相同");
                    return;
                }
                user.setPassword(newPW);
                user.setPasswdOld(oldPW);
                iPresenter.user_updatePassword(user);
                ShowMsgUtil.showLoadingDialog(this);
                break;
        }

    }

    @Subscribe()
    public void msgProcesse(BaseResponse object) {
        ShowMsgUtil.hideLoadingDialog();
        if (object.getCode() == 200) {
            showMsg("密码修改成功，请重新登录");
            Intent intent = new Intent(this, LoginAc.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }

    }


}
